/*
 * @Author: zxc
 * @Date: 2020-11-25 09:36:57
 * @LastEditTime: 2020-11-25 11:18:02
 * @LastEditors: zxc
 * @Description:状态模式
 */
// class Light {
//   constructor() {
//     this.state = "off"
//     this.button = null
//   }
//   init() {
//     let button = document.createElement("button")
//     let self = this
//     button.innerHTML = "开关"
//     this.button = document.body.appendChild(button)
//     this.button.onclick = function () {
//       self.buttonWasPressed()
//     }
//   }
//   buttonWasPressed() {
//     if (this.state === "off") {
//       console.log("弱光")
//       this.state = "weakLight"
//     } else if (this.state === "weakLight") {
//       console.log("强光")
//       this.state = "strongLight"
//     } else if (this.state === "strongLight") {
//       console.log("关灯")
//       this.state = "off"
//     }
//   }
// }

// let light = new Light()
// light.init()

// class OffLightState {
//   constructor(light) {
//     this.light = light
//   }
//   buttonWasPressed() {
//     console.log("弱光")
//     this.light.setState(this.light.weakLightState)
//   }
// }
// class WeakLightState {
//   constructor(light) {
//     this.light = light
//   }
//   buttonWasPressed() {
//     console.log("强光")
//     this.light.setState(this.light.strongLightState)
//   }
// }
// class StrongLightState {
//   constructor(light) {
//     this.light = light
//   }
//   buttonWasPressed() {
//     console.log("关灯")
//     this.light.setState(this.light.offLightState)
//   }
// }
// class Light {
//   constructor() {
//     this.offLightState = new OffLightState(this)
//     this.weakLightState = new WeakLightState(this)
//     this.strongLightState = new StrongLightState(this)
//     this.button = null
//   }
//   init() {
//     let button = document.createElement("button")
//     let self = this
//     this.currState = this.offLightState
//     button.innerHTML = "开关"
//     this.button = document.body.appendChild(button)
//     this.button.onclick = function () {
//       self.currState.buttonWasPressed()
//     }
//   }
//   setState(newState) {
//     this.currState = newState
//   }
// }
// let light = new Light()
// light.init()

// class Light {
//   constructor(FSM) {
//     this.currState = FSM.off
//     this.button = null
//   }
//   init() {
//     let button = document.createElement("button")
//     let self = this
//     button.innerHTML = "已关ossh"
//     this.button = document.body.appendChild(button)
//     this.button.onclick = function () {
//       self.currState.buttonWasPressed.call(self)
//     }
//   }
// }

// let FSM = {
//   off: {
//     buttonWasPressed() {
//       console.log("关灯")
//       this.button.innerHTML = "下一次按我是开灯"
//       this.currState = FSM.on
//     }
//   },
//   on: {
//     buttonWasPressed() {
//       console.log("开灯")
//       this.button.innerHTML = "下一次按我是关灯"
//       this.currState = FSM.off
//     }
//   }
// }
// var light = new Light(FSM)
// light.init()

let delegate = function (client, delegation) {
  return {
    buttonWasPressed() {
      return delegation.buttonWasPressed.apply(client, arguments)
    }
  }
}

let FSM = {
  off: {
    buttonWasPressed() {
      console.log("关灯")
      this.button.innerHTML = "下一次按我是开灯"
      this.currState = this.onState
    }
  },
  on: {
    buttonWasPressed() {
      console.log("开灯")
      console.log(this)
      this.button.innerHTML = "下一次按我是关灯"
      this.currState = this.onState
    }
  }
}

class Light {
  constructor() {
    this.offState = delegate(this, FSM.off)
    this.onState = delegate(this, FSM.on)
    this.currState = this.offState // 设置初始状态为关闭状态 this.button = null;
  }
  init() {
    let button = document.createElement("button")
    let self = this
    button.innerHTML = "已关ossh"
    this.button = document.body.appendChild(button)
    this.button.onclick = function () {
      self.currState.buttonWasPressed()
    }
  }
}
var light = new Light()
light.init()
